package cn.zr.aitool.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Description: DataBaseHelper.java Create on 2013-5-7 下午7:54:18
 *
 * @author wk
 * @version 1.0 Copyright (c) 2013 Company,Inc. All Rights Reserved.
 */
public class DataBaseHelper extends SQLiteOpenHelper {

    private final static int DATABASE_VERSION = 1;
    private static String DATABASE_NAME = "aitool.db";
    private static DataBaseHelper mOrgDataBaseHelperInstance = null;
    public static SQLiteDatabase mDatabase = null;
    public static final String TOOL_NAV_TEXT = "tool_nav_text";
    public static final String TALE_CONTENT = "content";
    private static Lock mDBLock;

    private DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public static DataBaseHelper getInstance(Context context) {
        if (mOrgDataBaseHelperInstance == null) {
            mOrgDataBaseHelperInstance = new DataBaseHelper(context);
            mDatabase = mOrgDataBaseHelperInstance.getWritableDatabase();
            mDBLock = new ReentrantLock();
        }
        return mOrgDataBaseHelperInstance;
    }

    public void lock() {
        mDBLock.lock();
    }

    public void unLock() {
        mDBLock.unlock();
    }

    public DataBaseHelper(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createToolNavText(db);
        createContent(db);
    }

    @SuppressLint("NewApi")
    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        super.onDowngrade(db, oldVersion, newVersion);
        onCreate(db);
    }

    private void createToolNavText(SQLiteDatabase db) {
        StringBuilder location_sql = new StringBuilder();
        location_sql.append("create table IF NOT EXISTS ").append(TOOL_NAV_TEXT).append("(")
                .append("id INTEGER PRIMARY KEY DEFAULT 0,")
                .append("text VARCHAR(128) NOT NULL DEFAULT (''),")
                .append("type TINYINT NOT NULL DEFAULT 0); ");
        try {
            db.execSQL(location_sql.toString());
        } catch (SQLException e) {
        }
    }

    private void createContent(SQLiteDatabase db) {
        StringBuilder location_sql = new StringBuilder();
        location_sql.append("create table IF NOT EXISTS ").append(TALE_CONTENT).append("(")
                .append("id INTEGER PRIMARY KEY DEFAULT 0,")
                .append("title VARCHAR(128) NOT NULL DEFAULT (''),")
                .append("content VARCHAR(128) NOT NULL DEFAULT (''),")
                .append("start VARCHAR(32) NOT NULL DEFAULT (''),")
                .append("end VARCHAR(32) NOT NULL DEFAULT (''),")
                .append("content_one VARCHAR(128) NOT NULL DEFAULT (''),")
                .append("content_two VARCHAR(128) NOT NULL DEFAULT (''),")
                .append("content_thr VARCHAR(128) NOT NULL DEFAULT (''),")
                .append("type TINYINT NOT NULL DEFAULT 0,")
                .append("position TINYINT NOT NULL DEFAULT 0);");
        try {
            db.execSQL(location_sql.toString());
        } catch (Exception e) {
            Log.i(DATABASE_NAME, Log.getStackTraceString(e));
        }
    }


    public void createTable(SQLiteDatabase db, String exeSql) {
        try {
            db.execSQL(exeSql);
        } catch (SQLException e) {
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public synchronized void execSQL(String sql) {
        try {
            mDatabase.execSQL(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public int delete(String table, String selection, String[] selectionArgs) {
        int count = -1;

        try {
            count = mDatabase.delete(table, selection, selectionArgs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // Logger.e("delete =" + count + ", selection " + selection +
        // ", selectionArgs " + selectionArgs[0]);
        return count;
    }


}



